package com.zc.study.thread.多线程第五章节.thread5_1_1;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.SimpleTimeZone;
import java.util.Timer;
import java.util.TimerTask;

public class Run {
    //运行完成之后线程宾没有终止,详细原因可看源代码
    //private static  Timer timer = new Timer();
    //将定时器设置为守护线程可以解决上述问题
    private static Timer timer = new Timer(true);
    static public class Task extends TimerTask{

        @Override
        public void run() {
            System.out.println("运行了!时间为"+new Date());
        }
    }

    public static void main(String[] args) {
        try {
            Task task = new Task();
            SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
            String dateString= "2018-3-14 14:50:00";
            Date date = sdf.parse(dateString);
            System.out.println("字符串时间"+date.toLocaleString()+"当前时间:"+new Date().toLocaleString());
            timer.schedule(task,date);
            Thread.sleep(60000);
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
